package com.ruoyi.DataManage.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.DataManage.domain.Subject_tree;
import com.ruoyi.DataManage.mapper.Subject_treeMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.DataManage.mapper.CategoryMapper;
import com.ruoyi.DataManage.domain.Category;
import com.ruoyi.DataManage.service.ICategoryService;

/**
 * 门类管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-11
 */
@Service
public class CategoryServiceImpl implements ICategoryService 
{
    @Autowired
    private CategoryMapper categoryMapper;


    /**
     * 查询门类管理
     * 
     * @param id 门类管理主键
     * @return 门类管理
     */
    @Override
    public Category selectCategoryById(Long id)
    {
        return categoryMapper.selectCategoryById(id);
    }

    /**
     * 查询门类管理列表
     * 
     * @param category 门类管理
     * @return 门类管理
     */
    @Override
    public List<Category> selectCategoryList(Category category)
    {
        return categoryMapper.selectCategoryList(category);
    }

    /**
     * 新增门类管理
     * 
     * @param category 门类管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCategory(Category category)
    {
        category.setCreateTime(DateUtils.getNowDate());
        int rows = categoryMapper.insertCategory(category);
        insertSubject(category);
        return rows;
    }

    /**
     * 修改门类管理
     * 
     * @param category 门类管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCategory(Category category)
    {
        category.setUpdateTime(DateUtils.getNowDate());
        //先根据门类Id删除该门类的所有属性
        categoryMapper.deleteSubject_treeByCategoryId(category.getId());
        List<Subject_tree> subjectList = category.getSubject_treeList();
        Long id = category.getId();
        if (StringUtils.isNotNull(subjectList))
        {
            List<Subject_tree> list = new ArrayList<>();
            for (Subject_tree subject : subjectList)
            {
                //设置学科为顶级父类
                subject.setParentId(0L);
                //设置类型为学科类型
                subject.setType(0L);
                subject.setCategoryId(id);
                //设置创建时间
                subject.setCreateTime(DateUtils.getNowDate());
                //设置更新时间
                subject.setUpdateTime(DateUtils.getNowDate());
                list.add(subject);
            }
            if (list.size() > 0)
            {
                categoryMapper.batchSubject_tree(list);
            }
        }
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除门类管理
     * 
     * @param ids 需要删除的门类管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCategoryByIds(Long[] ids)
    {
        categoryMapper.deleteSubject_treeByCategoryIds(ids);
        return categoryMapper.deleteCategoryByIds(ids);
    }

    /**
     * 删除门类管理信息
     * 
     * @param id 门类管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCategoryById(Long id)
    {
        categoryMapper.deleteSubject_treeByCategoryId(id);
        return categoryMapper.deleteCategoryById(id);
    }

    /**
     * 新增Subject信息
     * 
     * @param category 门类管理对象
     */
    public void insertSubject(Category category)
    {
        List<Subject_tree> subjectList = category.getSubject_treeList();
        Long id = category.getId();
        if (StringUtils.isNotNull(subjectList))
        {
            List<Subject_tree> list = new ArrayList<>();
            for (Subject_tree subject : subjectList)
            {
                //设置学科为顶级父类
                subject.setParentId(0L);
                //设置类型为学科类型
                subject.setType(0L);
                subject.setCategoryId(id);
                subject.setCreateTime(DateUtils.getNowDate());
                list.add(subject);
            }
            if (list.size() > 0)
            {
                categoryMapper.batchSubject_tree(list);
            }
        }
    }

    @Override
    public List<Category> getCategoryList(Long universityId) {
        if(universityId == -1) return getCategoryList();
        return categoryMapper.getCategoryListByUniversityId(universityId);
    }

    @Override
    public List<Category> getCategoryList() {
        return categoryMapper.getCategoryList();
    }
}
